﻿Imports System.Data.OleDb
Public Class frmCandidateManager

#Region "NotifyIcon"

    Private Sub frmCandidateManager_Resize(sender As Object, e As EventArgs) Handles Me.Resize
        Try
            If Me.WindowState = FormWindowState.Minimized Then
                Me.Visible = False
                NotifyIcon.Visible = True
                NotifyIcon.ShowBalloonTip(1, "SkyCoffee", "SkyCoffee is Still Running!", ToolTipIcon.Info)
            End If
        Catch ex As Exception
            MsgBox(ex.Message)
        End Try
    End Sub

    Private Sub NotifyIcon_MouseDoubleClick(sender As Object, e As MouseEventArgs) Handles NotifyIcon.MouseDoubleClick
        Try
            Me.Visible = True
            Me.WindowState = FormWindowState.Normal
            NotifyIcon.Visible = False
        Catch ex As Exception
            MsgBox(ex.Message)
        End Try
    End Sub

    Private Sub mnuLaunchApplication_Click(sender As Object, e As EventArgs) Handles mnuLaunchApplication.Click
        Try
            Me.Visible = True
            Me.WindowState = FormWindowState.Normal
            NotifyIcon.Visible = False
        Catch ex As Exception
            MsgBox(ex.Message)
        End Try
    End Sub

    Private Sub mnuExitApplication_Click(sender As Object, e As EventArgs) Handles mnuExitApplication.Click
        Application.Exit()
    End Sub
#End Region

    Private Sub frmCandidateManager_Load(sender As Object, e As EventArgs) Handles Me.Load
        'Loads frmMenu
        pnlIcons.Controls.Clear()
        pnlIcons.Controls.Add(frmMenu.pnlIcons)

        'Load ToolTips
        AdminToolTips()

        'Clears ListView before it is populated to avoid duplicate records
        lstDisplayCandidates.Items.Clear()

        'Populates ListView from Access Database
        Try
            PopulateCandidatesListFromAccess(lstDisplayCandidates)
        Catch ex As Exception
            MessageBox.Show(ex.ToString())
        End Try
    End Sub

    Private Sub tsbtnAdd_Click(sender As Object, e As EventArgs) Handles tsbtnAdd.Click
        frmManageCandidates.Show()
        frmManageCandidates.AddNewRecord()
        Me.Close()
    End Sub

    Private Sub tsbtnEdit_Click(sender As Object, e As EventArgs) Handles tsbtnEdit.Click
        frmManageCandidates.Show()
        Me.Close()
    End Sub

    Private Sub tsbtnDelete_Click(sender As Object, e As EventArgs) Handles tsbtnDelete.Click
        frmManageCandidates.Show()
        Me.Close()
    End Sub

    Private Sub btnSearch_Click(sender As Object, e As EventArgs) Handles btnSearch.Click
        If txtSearchReferenceNumber.Text IsNot "" And lstDisplayCandidates.Items.Count > 0 Then
            lstDisplayCandidates.Focus()

            For i = 0 To lstDisplayCandidates.Items.Count - 1
                'Searches first column: SubItems(0)
                If lstDisplayCandidates.Items(i).SubItems(0).Text = txtSearchReferenceNumber.Text Then
                    lstDisplayCandidates.Items(i).Selected = True
                End If
                MsgBox("Search Completed Successfully")
            Next

        ElseIf txtSearchFirstName.Text IsNot "" And lstDisplayCandidates.Items.Count > 0 Then
            lstDisplayCandidates.Focus()

            For i = 0 To lstDisplayCandidates.Items.Count - 1
                'Searches third column: SubItems(2)
                If lstDisplayCandidates.Items(i).SubItems(2).Text = txtSearchFirstName.Text Then
                    lstDisplayCandidates.Items(i).Selected = True
                End If
                MsgBox("Search Completed Successfully")
            Next

        ElseIf txtSearchSurname.Text IsNot "" And lstDisplayCandidates.Items.Count > 0 Then
            lstDisplayCandidates.Focus()

            For i = 0 To lstDisplayCandidates.Items.Count - 1
                'Searches fourth column: SubItems(3)
                If lstDisplayCandidates.Items(i).SubItems(3).Text = txtSearchSurname.Text Then
                    lstDisplayCandidates.Items(i).Selected = True
                End If
                MsgBox("Search Completed Successfully")
            Next

        ElseIf txtSearchReferenceNumber.Text = "" Or txtSearchFirstName.Text = "" Or txtSearchSurname.Text = "" Then
            MsgBox("Please enter some text to initiate a search.")
        End If
    End Sub

    Private Sub btnSearchClear_Click(sender As Object, e As EventArgs) Handles btnSearchClear.Click
        'Empty Search form
        txtSearchReferenceNumber.Text = ""
        txtSearchFirstName.Text = ""
        txtSearchSurname.Text = ""

        txtSearchReferenceNumber.Focus()
    End Sub

    Private Sub tsbtnSendEmail_Click(sender As Object, e As EventArgs) Handles tsbtnSendEmail.Click
        frmSendEmail.Show()
        Me.Close()
    End Sub

    Private Sub tsbtnSendSMS_Click(sender As Object, e As EventArgs) Handles tsbtnSendSMS.Click
        frmSendSMS.Show()
        Me.Close()
    End Sub
End Class